package com.cuit.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.BookMapper;
import com.cuit.mapper.OrderInfoMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.OrderInfo;
import com.cuit.result.R;
import com.cuit.service.FileService;
import com.cuit.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class orderController {
    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private FileService fileService;

    @GetMapping("/countOrder/{uId}")
    @ApiOperation("订单的条数")
    public R getOrderCount(@PathVariable Integer uId){
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uId);
        Integer orderCunt = orderInfoMapper.selectCount(queryWrapper);
        return R.ok().data("orderN",orderCunt);
    }
    @ApiOperation("用户订单未支付列表")
    @GetMapping("/getOderWei/{userId}")
    public R listByIdW(@PathVariable Long userId){
        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDescById(userId);
        List<OrderInfo> oderList= Lists.newArrayList();
        list.forEach(order->{
            Book book = bookMapper.selectById(order.getProductId());
            order.setOrderNo(fileService.allPath(book.getBookimg()));
            if(order.getOrderStatus().equals("未支付")){
                oderList.add(order);
            }
        });

        return R.ok().data("list", oderList);
    }
    @ApiOperation("用户订单退单列表")
    @PostMapping("/getOderTui")
    public R listByIdeiTui(){
        List<OrderInfo> list = null;
        return R.ok().data("list", list);
    }
    @ApiOperation("用户订单支付成功列表")
    @GetMapping("/getOrderCheng/{userId}")
    public R listByIdCheng(@PathVariable Long userId){
        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDescById(userId);
        List<OrderInfo> oderList= Lists.newArrayList();
        list.forEach(order->{
            Book book = bookMapper.selectById(order.getProductId());
            order.setOrderNo(fileService.allPath(book.getBookimg()));
            if(order.getOrderStatus().equals("支付成功")){
                oderList.add(order);
            }
        });
        return R.ok().data("list", oderList);
    }
}
